import React, { memo, useCallback } from 'react';
import { Button, message, Modal, Icon } from 'antd';
import _ from 'lodash';

import { isSuccess } from '@/utils/request';

import { financialRecheck } from '@/services/salaryManage/salaryCalculate';

const { passCheckBatch } = financialRecheck;

// 批量复核通过
const BatchApprove = memo(({ handleItem, year, organizationCode, month, selectedRows, updateStateFn }) => {
    const handleFn = useCallback(async () => {
        if (!selectedRows || selectedRows.length === 0) {
            message.warn('请选择批量通过的对象');
            return;
        }
        Modal.confirm({
            title: '批量确认',
            content: '确认要批量通过员工的薪资信息吗？',
            okText: '确定',
            cancelText: '取消',
            centered: true,
            icon: <Icon type="exclamation-circle" theme="filled" />,
            onOk: async () => {
                const passRes = await passCheckBatch({
                    year,
                    organizationCode,
                    month,
                    salaryPayrollIdList: _.map(selectedRows, 'id')
                })
                if (isSuccess(passRes)) {
                    updateStateFn(passRes)
                } else {
                    message.error(passRes.msg)
                }
            },
            onCancel() {
                console.log('Cancel');
            }
        });
    }, [month, organizationCode, selectedRows, updateStateFn, year]);


    return (
        <Button type="primary" key={handleItem.code} onClick={handleFn}>
            {handleItem.name}
        </Button>
    );
});

export default memo(BatchApprove)
